const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainId: null,
    courseTrains: [{
      name: '胸部训练',
      selected: false,
      items: []
    }, {
      name: '背部训练',
      selected: false,
      items: []
    }, {
      name: '手臂训练',
      selected: false,
      items: []
    }, {
      name: '臀部训练',
      selected: false,
      items: []
    }, {
      name: '腹部训练',
      selected: false,
      items: []
    }, {
      name: '腿部训练',
      selected: false,
      items: []
    }],
    positions: [{
      position: '二头',
      columnName: 'ertou',
      ertou1: 0,
      ertou2: 0
    }, {
      position: '背阔',
      columnName: 'beikuo',
      beikuo1: 0,
      beikuo2: 0
    }, {
      position: '三头',
      columnName: 'santou',
      santou1: 0,
      santou2: 0
    }, {
      position: '下背',
      columnName: 'xiabei',
      xiabei1: 0,
      xiabei2: 0
    }, {
      position: '胸部',
      columnName: 'xiongbu',
      xiongbu1: 0,
      xiongbu2: 0
    }, {
      position: '臀部',
      columnName: 'tunbu',
      tunbu1: 0,
      tunbu2: 0
    }, {
      position: '腹部',
      columnName: 'fubu',
      fubu1: 0,
      fubu2: 0
    }, {
      position: '前腿',
      columnName: 'qiantui',
      qiantui1: 0,
      qiantui12: 0
    }, {
      position: '上背',
      columnName: 'shangbei',
      shangbei1: 0,
      shangbei2: 0
    }, {
      position: '后腿',
      columnName: 'houtui',
      houtui1: 0,
      houtui2: 0
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainId = wx.getStorageSync("trainId");
    this.setData({
      trainId
    });
    const courseTrains = this.data.courseTrains;
    const positionTrains = app.globalData.positionTrains;
    courseTrains.forEach(function(train) {
      positionTrains.forEach(function(positionTrain) {
        if (train.name === positionTrain.name) {
          train.items = positionTrain.movements;
        }
      });
    });
    this.setData({
      courseTrains
    });
  },
  changeSelect: function(e) {
    const index = e.currentTarget.dataset.index;
    const courseTrains = this.data.courseTrains;
    courseTrains[index].selected = !courseTrains[index].selected;
    this.setData({
      courseTrains: courseTrains
    });
  },
  findModeItems: function(name) {
    const positionTrains = app.globalData.positionTrains;
    const size = positionTrains.length;
    for (let i = 0; i < size; i++) {
      const positionTrain = positionTrains[i];
      if (positionTrain.name === name) {
        return positionTrain.normal;
      }
    }
    return [];
  },
  addCourse: function() {
    const that = this;
    const trainId = this.data.trainId;
    const courseTrains = this.data.courseTrains;
    const url = app.globalData.url;
    const positionTrains = app.globalData.positionTrains;
    const courses = [];
    courseTrains.forEach(function(courseTrain) {
      if (courseTrain.selected) {
        const programs = [];
        // const modeItems = that.findModeItems(courseTrain.name);
        // courseTrain.items.forEach(function(item) {
        //   const items = [];
        //   modeItems.forEach(function(modeItem) {
        //     items.push({
        //       position: modeItem
        //     });
        //   });
        //   const program = {
        //     name: item,
        //     strongOption: 'NORMAL',
        //     items
        //   };
        //   programs.push(program);
        // });
        courses.push({
          name: courseTrain.name,
          programs
        });
      }
    });
    wx.request({
      url: url + '/train/saveCourseTrains',
      data: {
        trainId,
        courses
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 2
          });
          wx.showToast({
            title: '添加成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '添加课程失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})